/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import entity.Ownership;
import entity.Storage;
import java.io.Serializable;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import session.ModifyPlayerSessionBeanLocal;

/**
 *
 * @author antonio
 */
@Named(value = "modifyPlayerController")
@SessionScoped
public class ModifyPlayerControllerBean implements Serializable {

    @EJB
    private ModifyPlayerSessionBeanLocal modifyPlayer;

    public ModifyPlayerControllerBean() {
    }

    public String getPlayer() {
        return modifyPlayer.getPlayer().getUsername();
    }
    
    public void setPlayer(String username) {
        this.modifyPlayer.initPlayer(username);
    }

    public Collection<Storage> getStorages() {
        return modifyPlayer.getStorages();
    }
    
    public Collection<Ownership> getOwnership() {
        return modifyPlayer.getOwnership();
    }

    public void update() {
        modifyPlayer.update();
    }

    @PostConstruct
    private void init() {
        if (modifyPlayer.getPlayer() == null) {
            try {
                getPlayerData();
            } catch (ServletException ex) {
                Logger.getLogger(GameSessionControllerBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void getPlayerData() throws ServletException {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        String player = context.getRequestParameterMap().get("player");
        for (String s : context.getRequestParameterMap().keySet()) {
            System.out.println(s);
        }
        System.out.println("ModifyController: "+player);
        modifyPlayer.initPlayer(player);
    }

}
